package com.example.controller;

import com.example.common.Result;
import com.example.domain.thirdPartyQueryParam.ZhihuiyaKeywordSearchRequestParam;
import com.example.service.KeywordSearchService;
import com.example.util.HttpUtils;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.*;

/**
 * 用户可以通过输入关键词进行检索。
 */
@Slf4j
@RestController
@RequestMapping("/zhihuiya")
@CrossOrigin
public class KeywordSearchController {

    @Autowired
    private KeywordSearchService keywordSearchService;

    @GetMapping("/hello")
    public String hello() {
        return "hello";
    }

    /**
     * 根据apiKey和secretKey申请token进行访问
     * @return
     */
    @PostMapping("/auth")
    public Result getZhihuiyaInterfaceAuthInfo(@RequestBody Map<String, String> authRequest) {
//        if (StringUtils.isBlank(apiKey)) {
//            apiKey = "10695r6nfnngjxz3nzwh72o6ckvuz7sofuqb9a76p9g6s3i3";
//        }
//        if (StringUtils.isBlank(secretKey)) {
//            secretKey = "tegcab1qil9pg2kyj40g9owuavp04hu1n9ytpwd3rgf3wem4hgejuqnpawyub2n7";
//        }
        String apiKey = authRequest.get("apiKey");
        String secretKey = authRequest.get("secretKey");
        Map headers = new HashMap();
        headers.put("Content-Type", "application/x-www-form-urlencoded");

        Map requestBody = new HashMap();
        requestBody.put("grant_type", "client_credentials");

        return keywordSearchService.zhihuiyaAuthentication(apiKey, secretKey, headers, requestBody);
    }

    @PostMapping("/search")
    public Result zhihuiyaPatentSearchByKeyword(HttpServletRequest request, @RequestBody ZhihuiyaKeywordSearchRequestParam requestParam) {
        String token = request.getHeader("authorization");
        String apikey = request.getHeader("apikey");
        if (StringUtils.isBlank(token) || StringUtils.isBlank(apikey)) {
            return Result.error("未携带token或apikey，请检测相关信息");
        }
        if (StringUtils.isBlank(requestParam.getQueryText())) {
            // 因没有输入关键字而返回错误
            return Result.error("未输入要搜索的内容");
        }

        // 如果排序规则为空，赋上默认值
        if (ObjectUtils.isEmpty(requestParam.getSort())) {
            Map<String, String> map = new HashMap<>();
            map.put("field", "PBDT_YEARMONTHDAY");
            map.put("order", "DESC");
            List<Map<String, String>> sort = new ArrayList<>();
            sort.add(map);
            requestParam.setSort(sort);
        }

        Map headers = new HashMap();
        headers.put("Content-Type", "application/json");
        headers.put("authorization", "Bearer " + token);
        headers.put("X-PatSnap-Version", "1.0");

        Map querys = new HashMap();
        querys.put("apikey", apikey);

        return keywordSearchService.queryPatentInfo(requestParam, headers, querys);
    }


}
